package com.c2001.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.c2001.springboot.controller.request.BaseRequest;
import com.c2001.springboot.controller.request.UserBorrowRequest;
import com.c2001.springboot.dao.*;
import com.c2001.springboot.domain.*;
import com.c2001.springboot.enums.OperateTypeEnum;
import com.c2001.springboot.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class UserService implements IUserService {
    @Autowired
    UserDao userdao;

    @Autowired
    BorrowDao borrowDao;

    @Autowired
    private BookDao bookDao;


    @Autowired
    AdminDao admindao;

    @Autowired
    private LevelDao levelDao;

    @Autowired
    private DistributionDao distributionDao;

    @Autowired
    private LibPointDao libPointDao;

    @Autowired
    private BuyHistoryDao buyHistoryDao;



    @Override
    public List<User> getAllUser() {
        return userdao.listUsers();
    }

    public PageInfo<User> getPage(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<User> users = userdao.listByCondition(baseRequest);
        return new PageInfo<>(users);
    }

    public void addUser(User user) {
        //自动生成username,唯一
        user.setUsername(DateUtil.format(new Date(),"yyyyMMdd")+IdUtil.fastSimpleUUID());
        Admin admin = new Admin();
        admin.setUsername(user.getName());
        admin.setPassword(user.getStunumber());
        admin.setStunumber(user.getStunumber());
        admin.setLevel(0);
        admindao.addAdmin(admin);
        userdao.addUser(user);
        Level level = new Level();
        level.setUsername(user.getUsername());
        level.setLevel(0);
        level.setStatus(0);
        level.setCreateTime(new Date());
        level.setUpdateTime(new Date());

        levelDao.insertSelective(level);
    }

    @Override
    public User getById(Integer id) {
        return userdao.getById(id);
    }

    @Override
    public void update(User user) {
        user.setUpdatetime(new Date());
        userdao.update(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userdao.deleteById(id);
        return;
    }

    @Override
    public Boolean borrowBookByUser(UserBorrowRequest userBorrowRequest) {
        User user = userdao.borrowBookByUser(userBorrowRequest.getStuname(),userBorrowRequest.getStunumber());
        if(user == null)
            return false;
        else {
            return true;
        }
    }

    @Override
    public Boolean buying(UserBorrowRequest userBorrowRequest) throws Exception {
        LibPoint libPoint = new LibPoint();
        libPoint.setLogicCode(UUID.randomUUID().toString());
        libPoint.setType(OperateTypeEnum.buy.getCode());
        libPoint.setDesc(OperateTypeEnum.buy.getDesc());
        libPoint.setUser(userBorrowRequest.getStuname());
        libPoint.setCreateTime(new Date());
        libPoint.setUpdateTime(new Date());
        libPointDao.insert(libPoint);
        if (Objects.isNull(userBorrowRequest) || Objects.isNull(userBorrowRequest.getStuname())){
            return false;
        }
        User bookByUser = userdao.borrowBookByUser(userBorrowRequest.getStuname(), userBorrowRequest.getStunumber());
        Integer money = bookByUser.getMoney();
        money -= userBorrowRequest.getPrice();
        if (money < 0){
            return false;
//            throw new Exception("余额不够，请联系管理员充值");
        }

        Integer bookId = userBorrowRequest.getBookId();
        Book bookInfo = bookDao.getById(bookId);
        Borrow req = new Borrow();
        req.setName(bookInfo.getName());
        req.setAuthor(bookInfo.getAuthor());
        req.setPrice( userBorrowRequest.getPrice());
        req.setStunumber(userBorrowRequest.getStunumber());
        req.setStuname(userBorrowRequest.getStuname());
        req.setBuyed(1);
        req.setBuyedStr("1");
        req.setCreatetime(new Date());
        req.setBookId(bookId);

        borrowDao.add(req);
        userdao.buying(money,userBorrowRequest.getStuname(),userBorrowRequest.getStunumber());
        Distribution distribution = new Distribution();
        distribution.setLogicCode(UUID.randomUUID().toString());
        distribution.setStatus(0);
        distribution.setBusStatus(0);
        distribution.setAddress(userBorrowRequest.getAddress());
        distribution.setCreateTime(new Date());
        distribution.setUpdateTime(new Date());
        distribution.setBookId(userBorrowRequest.getBookId());
        distribution.setStuname(userBorrowRequest.getStuname());

        distributionDao.insert(distribution);
        BuyHistory buyHistory = new BuyHistory();
        buyHistory.setUsername(userBorrowRequest.getStuname());
        buyHistory.setStatus(0);
        buyHistory.setCreateTime(new Date());
        buyHistory.setUpdateTime(new Date());
        buyHistory.setBookId(bookId);
        buyHistory.setPrice(userBorrowRequest.getPrice());
        buyHistoryDao.insertSelective(buyHistory);
        return true;
    }


}
